<template>
  <div class="login-container">
    <el-card class="box-card">
      <el-tabs v-model="activeName" class="tabs">
        <!-- 登录 -->
        <el-tab-pane class="tab-items" label="登录" name="login">
          <el-form ref="loginForm" class="form" :model="formData" :rules="formRules">
            <el-form-item prop="username">
              <el-input v-model="formData.username" placeholder="请输入用户名" />
            </el-form-item>
            <el-form-item prop="password">
              <el-input
                :key="passwordType"
                ref="password"
                v-model="formData.password"
                :type="passwordType"
                placeholder="请输入密码"
                @keyup.enter.native="handleLogin"
              />
              <span class="show-pwd" @click="showPwd">
                <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
              </span>
            </el-form-item>
            <el-form-item>
              <el-button v-loading="loading" type="primary" @click="handleLogin">登录</el-button>
            </el-form-item>
          </el-form>
          <div class="other">
            <div class="other-title">其他登陆方式</div>
            <div class="other-center">
              <a href="#"><svg-icon icon-class="qq" /></a>
              <a href="#"><svg-icon icon-class="wechat" /></a>
            </div>
          </div>
        </el-tab-pane>
      </el-tabs>

    </el-card>
  </div>
</template>

<script>
import { mapActions } from 'vuex'
export default {
  name: 'Login',
  data() {
    return {
      activeName: 'login', // tab切换样式
      formData: {
        username: 'heike233',
        password: '123456'
      },
      formRules: {
        username: [
          { required: true, trigger: 'blur', message: '用户名不能为空' },
          { min: 5, max: 16, trigger: 'blur', message: '用户名长度必须在5~16位之间' }
        ],
        password: [
          { required: true, trigger: 'blur', message: '密码不能为空' },
          { min: 5, max: 16, trigger: 'blur', message: '密码长度必须在5~16位之间' }
        ]
      },

      passwordType: 'password',
      loading: false,
      showDialog: false
    }
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.$nextTick(() => {
          this.passwordType = ''
        })
      } else {
        this.$nextTick(() => {
          this.passwordType = 'password'
        })
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    ...mapActions(['user/login']),
    handleLogin() {
      this.$refs.loginForm.validate(async valid => {
        if (valid) {
          try {
            this.loading = true
            await this['user/login'](this.formData)
            this.$router.push('/')
            this.$message.success('登录成功')
          } catch (error) {
            console.log(error)
          } finally {
            //  不论执行try 还是catch  都去关闭转圈
            this.loading = false
          }
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;

  .box-card {
    width: 450px;
    height: 500px;
  }

  .tabs {
    ::v-deep .el-tabs__nav-wrap::after {
      height: 0;
    }

    ::v-deep .el-tabs__item {
      color: #333;
      font-size: 22px;
      font-weight: 500;

    }

    ::v-deep .el-tabs__active-bar {
      background-color: #ff5c00;
      width: 44px;
      height: 3px;
      border-radius: 3px;
    }

    .form {
      margin-top: 20px;
    }

    ::v-deep .el-input--medium .el-input__inner {
      height: 60px;
    }

    ::v-deep .el-button--primary {
      margin-top: 15px;
      background-color: #ff7e29;
      border: none;
      width: 100%;
      height: 60px;

    }

    .other {
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;

      .other-title {
        color: #aaa;
        font-size: 17px;
        font-weight: 400;
        line-height: 40px;
      }

      .other-center {
        display: flex;

        a {
          display: flex;
          align-items: center;
          justify-content: center;
          margin: 0 12px;
          width: 46px;
          height: 46px;
          border-radius: 46px;
          text-align: center;

          &:first-child {
            background-color: #18acfc;
          }

          &:last-child {
            background-color: #50b674;
          }

          .svg-icon {
            font-size: 25px;
            color: #fff;
          }
        }
      }
    }

    .show-pwd {
      position: absolute;
      right: 10px;
      top: 25%;
      font-size: 16px;
      cursor: pointer;
      user-select: none;
    }

  }

}
</style>
